var platfrom = "dy";
var link_type = "wx";
var copyText = "抖音无风险链接";

$(function() {
    mviewToastr("load","加载中,请稍等...");
    platfrom = $("#platfrom").val();
    var links_id = getQueryVariable("links_id");
    link_type = getQueryVariable("link_type");
    if (link_type == "kf") {
        if (links_id != "") {
            $("#kfid").val(links_id);
        }
    } else {
        if (links_id != "") {
            $("#links_id").val(links_id);
        }
    }
    setTimeout(function(argument) {
        platfromChange(platfrom);
    }, 300);

    $("#platfrom_list li").click(function(){
        platfrom = $(this).attr("value");
        platfromChange(platfrom);
    });

    $("#link_type li").click(function(){
        link_type = $(this).attr("value");
        renderLinkType();
    });
});

$(document).ready(function(){
    var clipboardCard = new ClipboardJS('#copyCardLink');
    clipboardCard.on('success', function(e) {
        mviewToastr("success",copyText+"复制成功，根据教程制作卡片即可!");
    });
});

function renderLinkType() {
    $("#link_type li").removeClass("active");
    $("#link_type li[value="+link_type+"]").addClass("active");
    $("#link_type_wx").hide();
    $("#link_type_kf").hide();
    if (link_type == "wx") {
        $("#link_type_wx").show();
        showDouyinCard(1);
    } else {
        $("#link_type_kf").show();
        showDouyinCard(2);
    }
}

function platfromChange(platfrom) {
    $("#platfrom_list li").removeClass("active");
    $("#platfrom_list li[value="+platfrom+"]").addClass("active");
    $(".card-douyin").hide();
    $(".card-zhihu").hide();
    $(".card-wechat").hide();
    $("#cardCopyLink").hide();
    if (platfrom == "dy") {
        $("#create_notice").text("抖音卡片制作教程").attr("href","https://www.tiantianwailian.com/news/1149.html");
        $("#card_describe_row").show();
        $(".card-douyin").show();
        // $("#cardSbumitBtn").text("复制抖音无风险提示链接");
        $("#cardSbumitBtn").text("生成抖音图文卡片");
        $("#douyin_ad").show();
        $("#doyin_notice").show();
        $("#weixin_notice").hide();
        $("#cardCopyLink").show();
    } else if (platfrom == "wx") {
        $("#create_notice").text("微信卡片制作教程").attr("href","https://www.tiantianwailian.com/news/1150.html");
        $("#card_describe_row").show();
        $(".card-wechat").show();
        $("#cardSbumitBtn").text("生成微信图文卡片");
        $("#douyin_ad").hide();
        $("#doyin_notice").hide();
        $("#weixin_notice").show();
    } else {
        $("#create_notice").text("知乎卡片制作教程").attr("href","https://www.tiantianwailian.com/news/1151.html");
        $("#card_describe_row").hide();
        $(".card-zhihu").show();
        $("#cardSbumitBtn").text("获取卡片链接");
        $("#douyin_ad").hide();
        $("#doyin_notice").hide();
        $("#weixin_notice").show();
    }
    renderLinkType();
}

function cardEditChange(obj) {
    var id = obj.id;
    var value = $("#" + id).val();
    if (id == "dycard_title") {
        $(".card-douyin .info h3").text(value);
        $(".card-zhihu h3").text(value);
        $(".card-wechat h3").text(value);
    } else if (id == "dycard_describe") {
        $(".card-douyin .info h5").text(value);
        // $(".card-zhihu h5").text(value);
        $(".card-wechat h5").text(value);
    } else if (id == "dycard_logo") {
        $(".card-douyin img").attr("src", value);
        $(".card-wechat img").attr("src", value);
        $(".card-zhihu img").attr("src", value);
    }
}

function getQueryVariable(variable) {
    let query = window.location.search.substring(1);
    let vars = query.split("&");
    for (let i = 0; i < vars.length; i++) { let pair = vars[i].split("="); if (pair[0] == variable) { return pair[1]; } }
    return false;
}

function showDouyinCard(status) {
    if (status == 2) {
        var links_id = $("#kfid").val();
        if (kfid == "") {
            mviewToastr("warning","当前暂未创建微信客服外链,请创建后再制作!");
            return;
        }
    } else {
        var links_id = $("#links_id").val();
        if (links_id == "") {
            mviewToastr("warning","当前暂未创建微信外链,请创建后再制作!");
            return;
        }
    }
    $.ajax({
        "url": "/admin/tool/card_detial",
        "type": "GET",
        data: {
            "links_id": links_id,
            "type":platfrom,
            "link_type":link_type,
        },
        success: function(res) {
            colsemviewToastr();
            if (res.status == "success") {
                $("#dycard_title").val(res.data.linksCard.title);
                $("#dycard_describe").val(res.data.linksCard.describe);
                $("#dycard_logo").val(res.data.linksCard.logo);
                $("#dycard_logo_show").attr("src", res.data.linksCard.logo);
                if (res.data.linksCard.title == "") {
                    $(".card-douyin img").attr("src", "https://res.wailian1.cn/uploads/20230824/6545084.jpg");
                    $(".card-douyin h3").text("未配置卡片标题");
                    $(".card-douyin h5").text("未配置卡片描述");
                    $("#dycard_logo").val("https://res.wailian1.cn/uploads/20230824/6545084.jpg");
                    $("#dycard_logo_show").attr("src", "https://res.wailian1.cn/uploads/20230824/6545084.jpg");
                    $(".card-zhihu img").attr("src", "https://res.wailian1.cn/uploads/20230824/6545084.jpg");
                    $(".card-zhihu h3").text("未配置卡片标题");
                    $(".card-wechat img").attr("src", "https://res.wailian1.cn/uploads/20230824/6545084.jpg");
                    $(".card-wechat h3").text("未配置卡片标题");
                    $(".card-wechat h5").text("未配置卡片描述");
                } else {
                    $(".card-douyin img").attr("src", res.data.linksCard.logo);
                    $(".card-douyin h3").text(res.data.linksCard.title);
                    $(".card-douyin h5").text(res.data.linksCard.describe);
                    $(".card-zhihu img").attr("src", res.data.linksCard.logo);
                    $(".card-zhihu h3").text(res.data.linksCard.title);
                    $(".card-wechat img").attr("src", res.data.linksCard.logo);
                    $(".card-wechat h3").text(res.data.linksCard.title);
                    $(".card-wechat h5").text(res.data.linksCard.describe);
                }
            } else {
                mviewToastr("error", res.msg);
            }
        },
        error: function(err) {
            mviewToastr("error", "网络异常,请稍后重试！");
        }
    });
}

function dyCardSubmit(status) {
    var dycard_title = $("#dycard_title").val();
    var dycard_describe = $("#dycard_describe").val();
    var dycard_logo = $("#dycard_logo").val();
    if (dycard_title.length < 3) {
        mviewToastr("error", "请输入3个字以上卡片标题!");
        return;
    }
    if (platfrom != "zh" && dycard_describe.length < 3) {
        mviewToastr("error", "请输入3个字以上卡片描述!");
        return;
    }
    if (dycard_logo == "") {
        mviewToastr("error", "请输入卡片Logo!");
        return;
    }
    if (link_type == "wx") {
        var links_id = $("#links_id").val();
    } else {
        var links_id = $("#kfid").val();
    }
    var channel = $("#channel_code").val();
    $.ajax({
        "url": "/admin/tool/card_update",
        "type": "GET",
        data: {
            "links_id": links_id,
            "title": dycard_title,
            "describe": dycard_describe,
            "logo": dycard_logo,
            "type": platfrom,
            "link_type":link_type,
        },
        success: function(res) {
            if (res.status == "success") {
                var paramas = "?channel="+channel;
                if (channel == "") {
                    var cardUrl = res.data.card_url;
                } else {
                    var cardUrl = res.data.card_url+paramas;
                }
                if (platfrom == "zh") {
                    if (channel == "") {
                        cardUrl += "?type=zh&link_type="+link_type;
                    } else {
                        cardUrl += "&type=zh&link_type="+link_type;
                    }
                } else if (platfrom == "wx") {
                    if (channel == "") {
                        cardUrl += "?type=wx&link_type="+link_type;
                    } else {
                        cardUrl += "&type=wx&link_type="+link_type;
                    }
                } else {

                }
                if (platfrom == "wx") {
                    getWxCardService(cardUrl);
                } else if(platfrom == "dy"){
                    var nowtime = Math.round(new Date().getTime()/1000).toString();
                    if (status == 1) {
                        if (cardUrl.indexOf("?")!=-1) {
                            cardUrl += "&status=create";
                        } else {
                            cardUrl += "?status=create";
                        }
                        getDyCardService(cardUrl);
                        // $("#copyCardLink").attr("data-clipboard-text",cardUrl);
                        // setTimeout(function() {
                        //     $("#copyCardLink").click();
                        // }, 100);
                    } else {
                        if (cardUrl.indexOf("?")!=-1) {
                            cardUrl += "&v="+nowtime;
                        } else {
                            cardUrl += "?v="+nowtime;
                        }
                        copyText = "抖音私信无风险链接";
                        $("#copyCardLink").attr("data-clipboard-text",cardUrl);
                        setTimeout(function() {
                            $("#copyCardLink").click();
                        }, 100);
                        // window.open('https://www.tiantianwailian.com/news/1149.html','_blank');
                    }
                } else {
                    copyText = "知乎卡片链接";
                    $("#copyCardLink").attr("data-clipboard-text",cardUrl);
                    setTimeout(function() {
                        $("#copyCardLink").click();
                    }, 100);
                }
            } else {
                mviewToastr("error", res.msg);
            }
        },
        error: function(err) {
            mviewToastr("error", "网络异常,请稍后重试！");
        }
    });
}

function getWxCardService(cardUrl) {
    $('#wxcardModal').modal('toggle');
    $("#wxcardModal #service_text").text("请使用微信扫码制作");
    $("#wxcardModal #service_time").text("如卡片链接无法打开,在后台重新制作即可");
    $("#wxcardModal #service_describe").text("制作成功后建议 收藏卡片 ,然后分享时从收藏中转发即可!");
    $("#wx-card-image").html("");
    var qrcode = new QRCode(document.getElementById("wx-card-image"), {
        text: cardUrl,
        width: 500,
        height: 500,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H
    });
}

function getDyCardService(cardUrl) {
    $('#wxcardModal').modal('toggle');
    $("#wxcardModal #service_text").text("请使用抖音扫码制作");
    $("#wxcardModal #service_time").text("请务必使用抖音APP扫码制作");
    $("#wxcardModal #service_describe").text("制作成功后 长按转发 即可分享到别的聊天或群聊中");
    $("#wx-card-image").html("");
    var qrcode = new QRCode(document.getElementById("wx-card-image"), {
        text: cardUrl,
        width: 500,
        height: 500,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H
    });
}

function choleWxCardService() {
    $('#wxcardModal').modal('toggle');
}

function showCardLogoModal() {
    var cardRecommendLists = ["https://res.wailian1.cn/uploads/20230824/6545084.jpg","https://res.wailian1.cn/uploads/20230824/8023534.jpg","https://res.wailian1.cn/uploads/20230824/9086611.jpg","https://res.wailian1.cn/uploads/20230824/10253731.jpg","https://res.wailian1.cn/uploads/20230824/530267217.jpg","https://res.wailian1.cn/uploads/20230824/7092897.jpg","https://res.wailian1.cn/uploads/20230824/8918168.jpg","https://res.wailian1.cn/uploads/20230824/10545343.jpg","https://res.wailian1.cn/uploads/20230824/540824629.jpg","https://res.wailian1.cn/uploads/20230824/7688518.jpg","https://res.wailian1.cn/uploads/20230824/9493148.jpg","https://res.wailian1.cn/uploads/20230824/10882877.jpg","https://res.wailian1.cn/uploads/20230824/550799446.jpg","https://res.wailian1.cn/uploads/20230824/7513493.jpg","https://res.wailian1.cn/uploads/20230824/8870738.jpg","https://res.wailian1.cn/uploads/20230824/10375899.jpg","https://res.wailian1.cn/uploads/20230824/560541777.jpg","https://res.wailian1.cn/uploads/20230824/7335359.jpg","https://res.wailian1.cn/uploads/20230825/7933602.jpg","https://res.wailian1.cn/uploads/20230825/400825690.jpg"];
    $(".m-media-images ul").html('');
    $.each(cardRecommendLists, function(key, cardRecommendList){
        $(".m-media-images ul").append('<li onclick="choiceCardImage('+key+')"><img src="'+cardRecommendList+'"></li>');
    });
    $('#cardLogoModal').modal('toggle');
}

function choiceCardImage(index) {
    var cardRecommendLists = ["https://res.wailian1.cn/uploads/20230824/6545084.jpg","https://res.wailian1.cn/uploads/20230824/8023534.jpg","https://res.wailian1.cn/uploads/20230824/9086611.jpg","https://res.wailian1.cn/uploads/20230824/10253731.jpg","https://res.wailian1.cn/uploads/20230824/530267217.jpg","https://res.wailian1.cn/uploads/20230824/7092897.jpg","https://res.wailian1.cn/uploads/20230824/8918168.jpg","https://res.wailian1.cn/uploads/20230824/10545343.jpg","https://res.wailian1.cn/uploads/20230824/540824629.jpg","https://res.wailian1.cn/uploads/20230824/7688518.jpg","https://res.wailian1.cn/uploads/20230824/9493148.jpg","https://res.wailian1.cn/uploads/20230824/10882877.jpg","https://res.wailian1.cn/uploads/20230824/550799446.jpg","https://res.wailian1.cn/uploads/20230824/7513493.jpg","https://res.wailian1.cn/uploads/20230824/8870738.jpg","https://res.wailian1.cn/uploads/20230824/10375899.jpg","https://res.wailian1.cn/uploads/20230824/560541777.jpg","https://res.wailian1.cn/uploads/20230824/7335359.jpg","https://res.wailian1.cn/uploads/20230825/7933602.jpg","https://res.wailian1.cn/uploads/20230825/400825690.jpg"];
    $("#dycard_logo_show").attr("src",cardRecommendLists[index]);
    $("#dycard_logo").val(cardRecommendLists[index]);
    $(".phone-container img").attr("src",cardRecommendLists[index]);
    $('#cardLogoModal').modal('toggle');
}

function hideCardLogoModal() {
    $('#cardLogoModal').modal('toggle');
}

function jsUploadCallBack() {
    // body...
}
